<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流水编号" prop="incomeSerial">
        <el-input v-model="queryParams.incomeSerial" placeholder="请输入流水编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderSerial">
        <el-input v-model="queryParams.orderSerial" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单支付流水号" prop="payOrderSerial">
        <el-input v-model="queryParams.payOrderSerial" placeholder="请输入订单支付流水号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="退单编号" prop="backOrderSerial">
        <el-input v-model="queryParams.backOrderSerial" placeholder="请输入退单编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收支状态" prop="incomeStatus">
        <el-select v-model="queryParams.incomeStatus" placeholder="收支状态" clearable>
          <el-option v-for="dict in dict.type.income_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
        <el-form-item label="收支类型" prop="incomeType">
          <el-select v-model="queryParams.incomeType" placeholder="收支类型" clearable>
            <el-option v-for="dict in dict.type.income_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="收支时间" prop="upTime">
          <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        </el-form-item>

      <el-form-item>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>


    <el-table v-loading="loading" :data="incomeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流水编号" align="left" prop="incomeSerial" />
      <el-table-column label="订单编号" align="left" prop="orderSerial" />
      <el-table-column label="订单支付流水号" align="left" prop="payOrderSerial" />
      <el-table-column label="退单编号" align="left" prop="backOrderSerial" />
      <el-table-column label="商家名称" align="left" prop="storeName" />
      <!-- 商家，代理商  ['merchants','provinceAgent','cityAgent','countAgent'] -->
      <el-table-column v-if="roles==='merchants'||roles==='provinceAgent'||roles==='cityAgent'||roles==='countAgent'"
        label="用户昵称" align="left" prop="nickName" />
      <el-table-column v-if="roles==='merchants'||roles==='provinceAgent'||roles==='cityAgent'||roles==='countAgent'"
        label="用户ID" align="left" prop="userId" />
      <!-- 平台 ['admin'] -->
      <el-table-column v-if="roles==='admin'" label="角色" align="left" prop="role">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.role" size="medium">
            <dict-tag :options="dict.type.radit_role" :value="scope.row.role" />
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column v-if="roles==='admin'" label="名称" align="left" prop="userName" />
      <el-table-column v-if="roles==='admin'" label="用户ID" align="left" prop="userId" />

      <el-table-column label="收支状态" align="left" prop="incomeStatus">
        <template slot-scope="scope">
          <el-tag size="medium">
            <dict-tag :options="dict.type.income_status" :value="scope.row.incomeStatus" />
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="分账状态" align="left" prop="accountFlag">
        <template slot-scope="scope">
          <!-- 未分帐 0、1
已提交 4、5
分账成功 2
分账失败 3 -->
          <el-tag v-if="scope.row.accountFlag=='0'||scope.row.accountFlag=='1'" type="warning">未分帐</el-tag>
          <el-tag v-else-if="scope.row.accountFlag=='4'||scope.row.accountFlag=='5'" type="danger">已提交</el-tag>
          <el-tag v-else-if="scope.row.accountFlag=='2'" type="success">分账成功</el-tag>
          <el-tag v-else-if="scope.row.accountFlag=='3'" type="info">分账失败</el-tag>
          <el-tag v-else type="info">暂无</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="收支类型" align="left" prop="incomeType">
        <template slot-scope="scope">
          <el-tag size="medium">
            <dict-tag :options="dict.type.income_type" :value="scope.row.incomeType" />
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="left" prop="incomeAmount">
        <template slot-scope="scope">
          <!-- income_status 收支状态（0-收入+号 1-支出-号） -->
          <span style="font-weight: bold;color: #f00;" v-if="scope.row.incomeStatus==='0'">+</span>
          <span style="font-weight: bold;color: #f00;" v-else-if="scope.row.incomeStatus==='1'">-</span>
          <span v-else></span>
          <span style="font-weight: bold;color: #f00;margin-left: 5px;">¥{{scope.row.incomeAmount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="登记分账时间" align="left" prop="registerTime" />
      <el-table-column label="分账时间" align="left" prop="accountTime" />
      <el-table-column label="收支时间" align="center" prop="incomeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.incomeTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderSerial">
          <el-input v-model="form.orderSerial" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="退单编号" prop="backOrderSerial">
          <el-input v-model="form.backOrderSerial" placeholder="请输入退单编号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="金额" prop="incomeAmount">
          <el-input v-model="form.incomeAmount" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="收支时间" prop="incomeTime">
          <el-date-picker clearable v-model="form.incomeTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择收支时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="逻辑删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="创建时间" prop="crTime">
          <el-date-picker clearable v-model="form.crTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="upTime">
          <el-date-picker clearable v-model="form.upTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import store from "@/store";
  import {
    listIncome,
    getIncome,
    delIncome,
    addIncome,
    updateIncome
  } from "@/api/system/income";

  export default {
    name: "Income",
    dicts: ['income_status', 'income_type', 'radit_role'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 收支明细表格数据
        incomeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          incomeSerial: null,
          orderSerial: null,
          backOrderSerial: null,
          nickName: null,
          incomeStatus: null,
          incomeType: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        dateRange: [],
        roles: store.getters.roles[0]
      };
    },
    created() {
      console.log(this.roles, 'this.roles')
      this.getList();
    },
    methods: {
      /** 查询收支明细列表 */
      getList() {
        this.loading = true;
        listIncome(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.incomeList = response.data;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          incomeSerial: null,
          orderSerial: null,
          backOrderSerial: null,
          userId: null,
          incomeStatus: null,
          incomeType: null,
          incomeAmount: null,
          incomeTime: null,
          nickName: null,
          delFlag: null,
          crTime: null,
          upTime: null,
          incomeField1: null,
          incomeField2: null,
          incomeField3: null,
          incomeField4: null,
          incomeField5: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
        this.dateRange = []
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.incomeSerial)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加收支明细";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const incomeSerial = row.incomeSerial || this.ids
        getIncome(incomeSerial).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改收支明细";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.incomeSerial != null) {
              updateIncome(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addIncome(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const incomeSerials = row.incomeSerial || this.ids;
        this.$modal.confirm('是否确认删除收支明细编号为"' + incomeSerials + '"的数据项？').then(function() {
          return delIncome(incomeSerials);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/income/export', {
          ...this.addDateRange(this.queryParams, this.dateRange)
        }, `income_${new Date().getTime()}.xlsx`)
      },
      addDateRange(params, dateRange) {
        let search = params;
        dateRange = Array.isArray(dateRange) ? dateRange : [];
        search['startDate'] = dateRange[0];
        search['endDate'] = dateRange[1];
        return search;
      }
    }
  };
</script>
